package com.sophon.pattern.observer.strategyAndProxy;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.sophon.pattern.observer.Observer;
import com.sophon.pattern.observer.classic.Mouse;

public class StrategyMouseProxy extends Mouse {
	private Map<String, Set<Observer>> observerMap;

	public StrategyMouseProxy() {
		observerMap = new HashMap<>();
	}

	public void onEvent(String eventName) {
		MouseEvent event = MouseEventEnum.valueOf(eventName).get();
		event.action();
		notifyObserver(observerMap.get(eventName));
	}

	@Override
	public void registerObserver(String eventName, Observer o) {
		Set<Observer> observers = observerMap.get(eventName);
		if (observers == null) {
			observers = new HashSet<>();
			observerMap.put(eventName, observers);
		}
		observers.add(o);
	}

	@Override
	public void removeObserver(String eventName, Observer o) {
		Set<Observer> observers = observerMap.get(eventName);
		if (observers == null || observers.isEmpty()) {
			return;
		}
		observers.remove(o);
	}
}
